<?php
	Class Md_product extends CI_Model{
		function insert($data){
			$this->db->insert('aodai',$data);
			return $this->db->insert_id();
		}
		function update($data,$where){
			foreach ($where as  $key => $value){
				$this->db->where($key,$value);
			}
			
			$this->db->update('aodai',$data);
		}
		function get_one($id){
			$query = $this->db->query("select * from aodai where id = $id");
			return $query->result();			
		}
		function get_all($limit){
			if (isset($limit)){
				$query = $this->db->query("select * from aodai limit $limit");
			}else{
				$query = $this->db->query("select * from aodai ");
			}
			
			return $query->result();			
		}
		function list_search($where,$limit,$start){
			
			$str_where = "";
			$this->db->start_cache();
			foreach ($where as  $key => $value){
				if ($value != ""){
					$this->db->where($key,$value);
				}				
			}
			$this->db->stop_cache();
			$total_rec=  $this->db->count_all_results('aodai');			
			
			$this->db->limit($limit,$start);
			$query = $this->db->get("aodai");			
			
			
			$data = array(
				"result" => $query -> result(),
				"total"  => $total_rec
			);
			$this->db->flush_cache();  
			
			return $data;
		}
		function list_by_type($type,$limit){
			if ($limit != ""){
				$query = $this->db->query("select * from aodai where type = $type limit $limit");
			}else{
				$query = $this->db->query("select * from aodai where type = $type");
			}
			if ($query ->num_rows > 0){
				return $query->result();
			}else{
				return "";
			}
		}
		function get_where_in($field,$arr){
			$this->db->where_in($field,$arr);
			
			$query = $this->db->get("aodai");
			return $query->result();
		}
		function get_best_sale(){
			$query = $this->db->query("select ma_aodai,count(soluong) as soluong ,ad.*
				from donhang_chitiet dhct
				inner join aodai ad ON ad.id = dhct.ma_aodai
				GROUP BY ma_aodai 
				ORDER BY soluong desc ");
			return $query->result();
		}
		function get_new_aodai(){
			$query = $this->db->query("select *
				from aodai
				where created_date >= SUBDATE(now(),INTERVAL  43 day) ");
			return $query->result();
		}
		function delete($str_id){
			$arr_id = explode(",", $str_id);
			$this->db->where_in("id",$arr_id);
			
			$this->db->delete("aodai");
		}
	} 
?>